package com.hakim.wfb.admin.filter;

import com.hakim.wfb.admin.enums.ErrorCodeEnum;
import com.hakim.wfb.bean.Role;
import com.hakim.wfb.dao.RoleDao;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.validate.ValidateFilter;
import com.hkfs.fundamental.validate.annotaion.ValidateFilterBean;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 */
@ValidateFilterBean
public class AddRoleFilter extends ValidateFilter<AddRoleFilter.AddRoleFilterParam> {

    @Autowired
    RoleDao roleDao;

    @Override
    protected void prcoess(AddRoleFilterParam param, ResultEx resultEx){
        Role queryParam = new Role();
        queryParam.setRoleName(param.getRoleName());
        int count = roleDao.count(queryParam);
        if(count > 0){
            resultEx.makeResult(ErrorCodeEnum.ROLE_NAME_EXISTS.getCode(), ErrorCodeEnum.ROLE_NAME_EXISTS.getMessage());
            return;
        }
        resultEx.makeSuccessResult();
    }

    public static class AddRoleFilterParam{
        @NotEmpty(message = "角色名不能为空")
        private String roleName;

        public String getRoleName() {
            return roleName;
        }

        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }
    }
}
